package major;

import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsumerBuilder {

    private Consumer<String,String> consumer;

    public ConsumerBuilder(String consumerName, List<String> topicNames) {
        Map<String, Object> configs = new HashMap<>();
        configs.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.0.7:9092");
        configs.put(ConsumerConfig.GROUP_ID_CONFIG,consumerName);
        configs.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,false);
        configs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        configs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());

        Consumer<String, String> consumer = new KafkaConsumer<>(configs);

        consumer.subscribe(topicNames);
        this.consumer =consumer;
    }

    public Consumer<String, String> build() {
        return consumer;
    }

}
